package cn.itcast.sailing.controller;


import cn.itcast.sailing.common.domain.RestResponse;
import cn.itcast.sailing.dto.VerificationInfo;
import cn.itcast.sailing.service.VerificationService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@Api(value = "验证码服务接口")
@RestController
public class VerificationController {

    @Autowired
    private VerificationService verificationService;

    private String key;

    @ApiOperation(value="生成验证信息", notes="生成验证信息")
    @ApiImplicitParams({@ApiImplicitParam(name = "phone", value = "手机号码", required = true, dataType = "String", paramType="query"),})
    @GetMapping(value = "/generate")
    public String  generateVerificationInfo(@RequestParam("phone")String phone){
        Map<String,Object> body = new HashMap<>();
        body.put("mobile",phone);
        VerificationInfo verificationInfo = verificationService.generateVerificationInfo("sms", body, 60);
        RestResponse<VerificationInfo> success = RestResponse.success(verificationInfo);
        key = success.getResult().getKey();
        return success.getResult().getKey();
        // key  success.getResult().getKey()
    }

    @ApiOperation(value="校验", notes="校验")
    @ApiImplicitParams({@ApiImplicitParam(name = "keys", value = "验证码", required = true, dataType = "String", paramType="query")})
    @GetMapping(value = "/verify")
    public boolean verify(String keys){
        Boolean isSuccess = verificationService.verify("sms",key,keys);
        return RestResponse.success(isSuccess).getResult();
    }

}
